package com.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Pojo.Account;
import com.Pojo.AccountInfo;
import com.Pojo.AcountFund;
import com.Service.AccountInfo_Service;
import com.Service.Account_Service;
import com.Service.AcountFund_Service;
import com.util.Encrypt;
import com.util.JsonUtils;
import com.util.StatceCode;
import com.util.Switc;

@Controller
@RequestMapping("/account/category")
public class Account_Controller {
	@Autowired
	private Account_Service account_Service;

	@Autowired
	private AccountInfo_Service accountInfo_Service;

	@Autowired
	private AcountFund_Service acountFund_Service;

	private static final Logger logger = Logger
			.getLogger(Account_Controller.class);

	@RequestMapping(value = "/save", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String createContentCategory(String userName, String password) {
		logger.debug("注册： 手机号为 ：" + userName + "密码为：" + password);
		if (userName != null && !userName.equals(null)) {
			if (password != null && !password.equals(null)) {
				Account selectByNameKey = account_Service
						.selectByNameKey(userName);
				if (selectByNameKey == null) {
					int insertAccount = account_Service.insertAccount(userName,Encrypt.md5(password));
					if (insertAccount == 1) {
						Account slAc = account_Service.selectByNameKey(userName);
						accountInfo_Service.insertAccountInfo(slAc);
						acountFund_Service.insertAcountFund(slAc);
						return JsonUtils.objectToJson(StatceCode.MapCode(10001,"注册成功", null));
					} else {
						return JsonUtils.objectToJson(StatceCode.MapCode(10001,"注册失败", null));
					}
				} else {
					return JsonUtils.objectToJson(StatceCode.MapCode(10001,"该手机已被注册", null));
				}
			} else {
				return JsonUtils.objectToJson(StatceCode.MapCode(10001,"密码 不能为空", null));
			}
		} else {
			return JsonUtils.objectToJson(StatceCode.MapCode(10001, "手机号 不能为空",null));
		}
	}

	@RequestMapping(value = "/login", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String Login(String userName, String password) {
		logger.debug("登录： 手机号为 ：" + userName + "密码为：" + password);
		if (userName != null && !userName.equals(null)) {
			if (password != null && !password.equals(null)) {
				Map<String, Object> restMap = new HashMap<String, Object>();
				Account slAc = account_Service.selectByNameKey(userName);
				if (slAc == null) {
					return JsonUtils.objectToJson(StatceCode.MapCode(10001,
							"手机号码输入错误", null));
				} else if (slAc.getPassword().equals(Encrypt.md5(password))) {
					AccountInfo Infoselect = accountInfo_Service
							.selectByCount(slAc.getId());
					AcountFund fundSelect = acountFund_Service
							.selectBycount(slAc.getId());
					List<Object> list = new ArrayList<Object>();
					list.add(Switc.info(Infoselect));
					list.add(Switc.fund(fundSelect));
					return JsonUtils.objectToJson(StatceCode.MapCode(10001,
							"获取成功", list));
				} else {
					return JsonUtils.objectToJson(StatceCode.MapCode(10001,
							"密码输入错误", null));
				}
			} else {
				return JsonUtils.objectToJson(StatceCode.MapCode(10001,
						"密码 不能为空", null));
			}
		}
		return JsonUtils.objectToJson(StatceCode.MapCode(10001, "手机号 不能为空",
				null));
	}

	@RequestMapping(value = "/updatepwd", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String updateAccountPwd(int id, String pwd, String password) {
		Account select = account_Service.selectByPrimaryKey(id);
		if (select == null) {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"id不存在", null));
		}
		if (pwd != null && !pwd.equals(null)) {

		} else {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"旧密码不能为空", null));
		}
		if (password != null && !password.equals(null)) {

		} else {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"新密码不能为空", null));
		}
		if (!select.getPassword().equals(Encrypt.md5(pwd))) {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"旧密码输入错误", null));
		}
		int up = account_Service.updateAccountPwd(select.getId(), password);
		System.out.println(up);
		if (up != 1) {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"修改密码因为未知原因出错", null));
		}
		return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001, "修改密码成功",
				null));
	}

	@RequestMapping(value = "/updateaccount", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String updateAccount(Account account) {
		int up = 0;
		try {
			if (account.getPassword() != null
					&& !account.getPassword().equals(null)) {
				account.setPassword(Encrypt.md5(account.getPassword()));
			}
			account.setUpdatetime(new Date());
			up = account_Service.updateAccount(account);
		} catch (Exception e) {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"传入参数错误", null));
		}
		if (up == 1) {
			return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001,
					"修改成功", null));
		}
		return JsonUtils.objectToJson(StatceCode.MapCodeEntity(10001, "修改失败",
				null));
	}
}
